package com.rs.controller;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.rs.model.ZhUser;
import com.rs.service.ZhUserTeacherService;
import com.rs.util.SpringContextUtils;

import java.awt.Toolkit;


@SuppressWarnings("all")
public class SelectTeacher extends JInternalFrame {
	

	
	private JPanel contentPane;

	List<JButton> jbutton = new ArrayList<>();
	
	JButton button_1 = new JButton("");
	JButton button_2 = new JButton("");
	JButton button_3 = new JButton("");
	JButton button_4 = new JButton("");
	JButton button_5 = new JButton("");
	JButton button_6 = new JButton("");
	JButton button_7 = new JButton("");
	JButton button_8 = new JButton("");
	JButton button_9 = new JButton("");
	JButton button_10 = new JButton("");
	
	int pageSum=-1;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectTeacher frame = new SelectTeacher();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void aa(){
		setBounds(100, 100, 630, 502);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton button = new JButton("\u4E0A\u4E00\u9875");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		button.setBounds(289, 419, 93, 23);
		contentPane.add(button);
		
		JButton button_01 = new JButton("\u4E0B\u4E00\u9875");
		button_01.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPage();
			}
		});
		button_01.setBounds(458, 419, 93, 23);
		contentPane.add(button_01);
		
	
		
	
		
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_1.getText();
				String []testid=test.split("\t");
				if(Integer.parseInt(testid[0]) != 0) {
					selectTeacher(Integer.parseInt(testid[0]));
				}else {
					
				}
				
			}
		});
		
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_2.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_3.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_4.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_5.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_6.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_7.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_8.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_9.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		button_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String test = button_10.getText();
				String []testid=test.split("\t");
				selectTeacher(Integer.parseInt(testid[0]));
			}
		});
		
		
		
		
		
		button_1.setHorizontalAlignment(SwingConstants.LEFT);
		button_1.setFont(new Font("等线", Font.PLAIN, 16));
		button_1.setBackground(Color.WHITE);
		button_1.setBounds(27, 77, 543, 34);
		contentPane.add(button_1);
		
		button_2.setHorizontalAlignment(SwingConstants.LEFT);
		button_2.setFont(new Font("等线", Font.PLAIN, 16));
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(27, 109, 543, 34);
		contentPane.add(button_2);
		
		button_3.setHorizontalAlignment(SwingConstants.LEFT);
		button_3.setFont(new Font("等线", Font.PLAIN, 16));
		button_3.setBackground(Color.WHITE);
		button_3.setBounds(27, 140, 543, 34);
		contentPane.add(button_3);
		
		button_4.setHorizontalAlignment(SwingConstants.LEFT);
		button_4.setFont(new Font("等线", Font.PLAIN, 16));
		button_4.setBackground(Color.WHITE);
		button_4.setBounds(27, 171, 543, 34);
		contentPane.add(button_4);
		
		button_5.setHorizontalAlignment(SwingConstants.LEFT);
		button_5.setFont(new Font("等线", Font.PLAIN, 16));
		button_5.setBackground(SystemColor.textHighlightText);
		button_5.setBounds(27, 204, 543, 34);
		contentPane.add(button_5);
		
		button_6.setHorizontalAlignment(SwingConstants.LEFT);
		button_6.setFont(new Font("等线", Font.PLAIN, 16));
		button_6.setBackground(Color.WHITE);
		button_6.setBounds(27, 237, 543, 34);
		contentPane.add(button_6);
		
		button_7.setHorizontalAlignment(SwingConstants.LEFT);
		button_7.setFont(new Font("等线", Font.PLAIN, 16));
		button_7.setBackground(Color.WHITE);
		button_7.setBounds(27, 270, 543, 34);
		contentPane.add(button_7);
		
		button_8.setHorizontalAlignment(SwingConstants.LEFT);
		button_8.setFont(new Font("等线", Font.PLAIN, 16));
		button_8.setBackground(Color.WHITE);
		button_8.setBounds(27, 301, 543, 34);
		contentPane.add(button_8);
		
		button_9.setHorizontalAlignment(SwingConstants.LEFT);
		button_9.setFont(new Font("等线", Font.PLAIN, 16));
		button_9.setBackground(Color.WHITE);
		button_9.setBounds(27, 333, 543, 34);
		contentPane.add(button_9);
		
		button_10.setHorizontalAlignment(SwingConstants.LEFT);
		button_10.setFont(new Font("等线", Font.PLAIN, 16));
		button_10.setBackground(Color.WHITE);
		button_10.setBounds(27, 365, 543, 34);
		contentPane.add(button_10);
		nextPage();
		
		JLabel lLabel_username = new JLabel("昵称");
		lLabel_username.setFont(new Font("等线", Font.PLAIN, 18));
		lLabel_username.setBounds(442, 44, 64, 34);
		contentPane.add(lLabel_username);
		
		JLabel label = new JLabel("性别");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("等线", Font.PLAIN, 18));
		label.setBounds(289, 44, 64, 34);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("等线", Font.PLAIN, 18));
		label_1.setBounds(145, 44, 64, 34);
		contentPane.add(label_1);
		
		JLabel lblId = new JLabel("  id");
		lblId.setHorizontalAlignment(SwingConstants.LEFT);
		lblId.setFont(new Font("等线", Font.PLAIN, 18));
		lblId.setBounds(37, 44, 64, 34);
		contentPane.add(lblId);
		
	}
	
	public void nextPage(){
		pageSum++;
		
		
		List<ZhUser> teacher = getTeachers(pageSum);
		if(teacher.size() == 0){
			JOptionPane.showMessageDialog(this, "当前已是左后一页！");
			pageSum--;
			List<ZhUser> teachers = getTeachers(pageSum);
			setTheValue(teacher);
				return;
		}
		setTheValue(teacher);
		
	}
	public void setTheValue(List<ZhUser> teacher){
		jbutton.clear();
		jbutton.add(button_1);
		jbutton.add(button_2);
		jbutton.add(button_3);
		jbutton.add(button_4);
		jbutton.add(button_5);
		jbutton.add(button_6);
		jbutton.add(button_7);
		jbutton.add(button_8);
		jbutton.add(button_9);
		jbutton.add(button_10);
		int sum = 0;
		for(ZhUser tt :teacher){
			System.out.println(tt);
			if(tt != null) {
				System.out.println(jbutton);
			jbutton.get(sum).setText(tt.getuId()+"\t                          \t"+
							 tt.getuName()+"\t                           \t"+
							 tt.getuSex()+"\t                              \t"+
							 tt.getuUsername());
			sum++;
			}
		}
	}
	
	public void upPage(){
		if(pageSum<=0){
			JOptionPane.showMessageDialog(this, "当前是第一页！");
			List<ZhUser> teacher = getTeachers(pageSum);
			setTheValue(teacher);
			return;
		}
		pageSum--;
		
//		jbutton.add(button_1);
//		jbutton.add(button_2);
//		jbutton.add(button_3);
//		jbutton.add(button_4);
//		jbutton.add(button_5);
//		jbutton.add(button_6);
//		jbutton.add(button_7);
//		jbutton.add(button_8);
//		jbutton.add(button_9);
//		jbutton.add(button_10);
		List<ZhUser> teacher = getTeachers(pageSum);
		setTheValue(teacher);
		
	}
	
	

	/**
	 * Create the frame.
	 */
	public SelectTeacher() {
		setClosable(true);
		setMaximizable(true);
		setIconifiable(true);
		setTitle("教室列表");
		aa();
	}
	
	public void selectTeacher(int id){
		new AddTeacher(1,id).setVisible(true);
	}
	
	
	private List<ZhUser> getTeachers(int pageSum){
		ZhUser zz=new ZhUser();
		zz.setuPoid(2);
		zz.setPageNum(pageSum);
		
		
		ZhUserTeacherService zUserTeacherService = SpringContextUtils.getBean(ZhUserTeacherService.class);
		
		
		Page<ZhUser> page = zUserTeacherService.selectTeachers(zz);
		List<ZhUser> zhUsers = new ArrayList<ZhUser>();
		for (ZhUser zhUser : page) {
			zhUsers.add(zhUser);
		}
		return zhUsers;
	}

}
